"""
检查数据库中用户记录的完整结构，特别是username字段
"""
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import settings

async def check_user_structure():
    # 连接数据库
    client = AsyncIOMotorClient(settings.MONGODB_URL)
    db = client[settings.MONGODB_DATABASE]
    
    # 查询所有用户
    users = await db.users.find().to_list(length=None)
    
    print("数据库中用户记录的完整结构:")
    for i, user in enumerate(users):
        print(f"\n用户 {i+1}:")
        print(f"  _id: {user.get('_id')}")
        print(f"  name: {user.get('name')}")
        print(f"  email: {user.get('email')}")
        print(f"  username: {user.get('username', '*** 字段不存在 ***')}")
        print(f"  status: {user.get('status')}")
        print(f"  is_verified: {user.get('is_verified')}")
        print(f"  roles: {user.get('roles', [])}")
        print(f"  permissions: {user.get('permissions', [])}")
        print(f"  department: {user.get('department')}")
        print(f"  position: {user.get('position')}")
        print(f"  phone: {user.get('phone')}")
        print(f"  avatar: {user.get('avatar')}")
        print(f"  created_at: {user.get('created_at')}")
        print(f"  updated_at: {user.get('updated_at')}")
        print(f"  last_login: {user.get('last_login')}")
        print(f"  login_count: {user.get('login_count')}")
        print("  ---")
    
    # 检查是否所有用户都有username字段
    users_without_username = [user for user in users if 'username' not in user]
    if users_without_username:
        print(f"\n发现 {len(users_without_username)} 个用户缺少username字段:")
        for user in users_without_username:
            print(f"  - {user.get('name')} ({user.get('email')})")
    else:
        print("\n所有用户都有username字段")
    
    client.close()

if __name__ == "__main__":
    asyncio.run(check_user_structure())